

import 'package:flutter/foundation.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/presentation/sports/add_sport/bloc/add_sport_page_screen.dart';
import 'package:syria_sports/presentation/sports/sports_page/bloc/sports_cubit.dart';
import '../../../../data/controllers/gyms_controller.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../models/sport.dart';

class SportItemView extends StatelessWidget {
  final Sport sport;
  final bool isSelected;
  final bool selectOnTap;
  final Function? onTapped;

  const SportItemView({
    super.key,
    required this.sport,
    this.isSelected = false,
    this.selectOnTap = false,
    this.onTapped,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (selectOnTap) {
          if (SportsCubit.get(context).selectMode) {
            SportsCubit.get(context).add(
              SelectSportEvent(
                sport: sport,
                isSelected: !isSelected,
              ),
            );
          } else {
            SportsCubit.get(context)
                .add(SelectModeEvent(mode: true, sport: sport));
          }
        } else {
          if (onTapped == null) {
            /*Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => AddSportPageScreen(
                          sport: sport,
                        )));*/
          }else{
            onTapped?.call();
          }
        }
      },
      onLongPress: () {
        if (SportsCubit.get(context).selectMode) {
          SportsCubit.get(context).add(
            SelectSportEvent(
              sport: sport,
              isSelected: !isSelected,
            ),
          );
        } else {
          SportsCubit.get(context)
              .add(SelectModeEvent(mode: true, sport: sport));
        }
      },
      child: Stack(
        children: [
          Card(
            color: Theme.of(context).colorScheme.secondary,
            child: Container(
              padding: const EdgeInsets.all(4),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  if (sport.image != null)
                    Image.network(
                      sport.image!,
                      height: 45,
                    )
                  else
                    Image.asset(
                      ImageConstant.imgLogo,
                      height: 45,
                    ),
                  Expanded(
                    child: Center(
                      child: Text(
                        sport.sportName,
                        maxLines: 2,
                        textAlign: TextAlign.center,
                        style: theme.textTheme.bodyMedium?.copyWith(fontSize: 14.fSize),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          if (SportsCubit.get(context).selectMode)
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Checkbox(
                  value: isSelected,
                  onChanged: (value) {
                    SportsCubit.get(context).add(
                      SelectSportEvent(
                        sport: sport,
                        isSelected: value ?? false,
                      ),
                    );
                  },
                ),
              ],
            ),
        ],
      ),
    );
  }

  void showDeleteConfirmation(BuildContext context, Sport myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: myGym.sportName,
          titleUser: 'الرياضة',
          onConfirm: () {
            GymsController.deleteById(sport.sportId ?? "-1");
            if (kDebugMode) {
              print('تم حذف النادي ${sport.sportName}!');
            }
          },
        );
      },
    );
  }
}
